﻿using Dapper;
using SIG.DAL.Dapper.Model;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;

namespace SIG.DAL.Dapper.Repositories
{

    public class ArticleCategoryRepository
    {
        private DbConnection _connection;
        public ArticleCategory GetItemBySeoName(string seoName)
        {
            ArticleCategory result;
            using (_connection = Utilities.GetOpenConnection())
            {
               result = _connection.GetList<ArticleCategory>(new { SeoName = seoName }).FirstOrDefault();               
            }
            return result;
        }

        public bool IsExistSeoName(string seoName)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                seoName = seoName.ToLower();
                result = _connection.RecordCount<ArticleCategory>("Where SeoName = @SeoName", new { SeoName = seoName });                
            }
            return result > 0;

        }
        public bool IsExistSeoName(string seoName,int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                seoName = seoName.ToLower();
                result = _connection.RecordCount<ArticleCategory>("Where SeoName = @SeoName and Id <> @Id", new { SeoName = seoName, Id=id });
            }
            return result > 0;
        }

        public IEnumerable<ArticleCategory> GetItems()
        {
            IEnumerable<ArticleCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<ArticleCategory>();
            }
            return result;
        }

        public IEnumerable<ArticleCategory> GetActiveItems()
        {
            IEnumerable<ArticleCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<ArticleCategory>(new { Active = true });
            }
            return result;
        }

        public IEnumerable<ArticleCategory> GetListPagedItems(int pageIndex, int pageSize,string keyword, out int totalCount)
        {            
            IEnumerable<ArticleCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                totalCount = _connection.RecordCount<ArticleCategory>("where Title like @Title", new { Title = likeKeyword });
                result = _connection.GetListPaged<ArticleCategory>(pageIndex, pageSize, "where Title like @Title",  "Importance desc" , new { Title = likeKeyword });
            }
            return result;
        }

        public ArticleCategory GetById(int id)
        {
            ArticleCategory result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<ArticleCategory>(id);
            }
            return result;
        }
        public int? Create(ArticleCategory model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }
        
        public int Update(ArticleCategory model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(ArticleCategory model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<ArticleCategory>(id);
            }
            return result;
        }


    }
}
